package sundun.sharding;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import sundun.sharding.abnormal.service.impl.MigrationFailServiceImpl;
import sundun.sharding.properties.MigrationTableProperties;
import sundun.sharding.scaner.SourceModelScaner;
import sundun.sharding.task.DataMigrationTask;

import javax.sql.DataSource;

@Configurable
@Import({DataMigrationTask.class, SourceModelScaner.class,
        MigrationFailServiceImpl.class})
@EnableConfigurationProperties({MigrationTableProperties.class})
public class MigrationConfiguration {
    private static final Logger log = LoggerFactory.getLogger(MigrationConfiguration.class);


    /**
     * 我们自定义一个 RedisTemplate，设置序列化器，这样我们可以很方便的操作实例对象。
     * 否则redis自动使用对象的jdk序列化 获取不到 key value
     */
    @Bean
    @ConditionalOnMissingBean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {


        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        //为了自己开发开发方便，一般直接使用<String, Serializable> 类型
//        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //value序列化
//        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //key序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

    /**
     * 初始化sql
     */
    @Value("classpath:sql/init.sql")
    private Resource sqlInit;

    @Bean
    public DataSourceInitializer dataSourceInitializer(final DataSource dataSource){
        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(dataSource);
        dataSourceInitializer.setDatabasePopulator(databasePopulator());
        log.info("初始化-迁移失败记录表成功！");
        return dataSourceInitializer;
    }

    private DatabasePopulator databasePopulator(){
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(sqlInit);
        resourceDatabasePopulator.setSeparator(";");
        return resourceDatabasePopulator;
    }

}
